<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>找回密码</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport"
          content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" href="../../layuiadmin/layui/css/layui.css" media="all">
    <link rel="stylesheet" href="../../layuiadmin/style/admin.css" media="all">
    <link rel="stylesheet" href="../../layuiadmin/style/login.css" media="all">
</head>

<div class="layadmin-user-login layadmin-user-display-show" id="LAY-user-login" style="display: none;">
    <div class="layadmin-user-login-main">
        <div class="layadmin-user-login-box layadmin-user-login-header">
            <h2>后台管理系统</h2>
            <p>管理系统</p>
        </div>
        <div class="layadmin-user-login-box layadmin-user-login-body layui-form">
            <script type="text/html" template>
                @{{# if(layui.router().search.type === 'resetpass'){ }}
                <div class="layui-form-item">
                    {{csrf_field()}}
                    <label class="layadmin-user-login-icon layui-icon layui-icon-password"
                           for="LAY-user-login-password"></label>
                    <input type="hidden" name="phone" value="{{request('phone')}}">
                    <input type="hidden" name="token" value="{{request('token')}}">
                    <input type="password" name="password" id="LAY-user-login-password" lay-verify="pass"
                           placeholder="新密码" class="layui-input">
                </div>
                <div class="layui-form-item">
                    <label class="layadmin-user-login-icon layui-icon layui-icon-password"
                           for="LAY-user-login-repass"></label>
                    <input type="password" name="repass" id="LAY-user-login-repass" lay-verify="required"
                           placeholder="确认密码" class="layui-input">
                </div>
                <div class="layui-form-item">
                    <button class="layui-btn layui-btn-fluid" lay-submit lay-filter="LAY-user-forget-resetpass"
                            id="LAY-user-forget-resetpass">
                        重置新密码
                    </button>
                </div>
                @{{# } else { }}
                <div class="layui-form-item">
                    {{csrf_field()}}
                    <label class="layadmin-user-login-icon layui-icon layui-icon-cellphone"
                           for="LAY-user-login-cellphone"></label>
                    <input type="text" name="phone" id="phone" lay-verify="phone"
                           placeholder="请输入注册时的手机号" class="layui-input">
                </div>
                <div class="layui-form-item">
                    <div class="layui-row">
                        <div class="layui-col-xs7">
                            <label class="layadmin-user-login-icon layui-icon layui-icon-vercode"
                                   for="LAY-user-login-vercode"></label>
                            <input type="text" name="captcha" id="captcha" lay-verify="required"
                                   placeholder="图形验证码" class="layui-input">
                        </div>
                        <div class="layui-col-xs5">
                            <div style="margin-left: 10px;">
                                <img src="{{captcha_src()}}"
                                     class="layadmin-user-login-codeimg" id="get-captcha">
                            </div>
                        </div>
                    </div>
                </div>
                <div class="layui-form-item">
                    <div class="layui-row">
                        <div class="layui-col-xs7">
                            <label class="layadmin-user-login-icon layui-icon layui-icon-vercode"
                                   for="LAY-user-login-smscode"></label>
                            <input type="text" name="vercode" id="LAY-user-login-smscode" lay-verify="required"
                                   placeholder="短信验证码" class="layui-input">
                        </div>
                        <div class="layui-col-xs5">
                            <div style="margin-left: 10px;">
                                <button type="button" class="layui-btn layui-btn-primary layui-btn-fluid"
                                        id="get-sms-code">获取验证码
                                </button>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="layui-form-item">
                    <button class="layui-btn layui-btn-fluid" lay-submit lay-filter="LAY-user-forget-submit"
                            id="LAY-user-forget-submit">找回密码
                    </button>
                </div>
                @{{# } }}
            </script>

        </div>
    </div>

    <div class="layui-trans layadmin-user-login-footer">

        <p>© 2018 <a href="http://www.layui.com/" target="_blank">layui.com</a></p>
        <p>
            <span><a href="http://www.layui.com/admin/#get" target="_blank">获取授权</a></span>
            <span><a href="http://www.layui.com/admin/pro/" target="_blank">在线演示</a></span>
            <span><a href="http://www.layui.com/admin/" target="_blank">前往官网</a></span>
        </p>
    </div>

</div>

<script src="../../layuiadmin/layui/layui.js"></script>
<script>
    layui.config({
        base: '../../layuiadmin/' //静态资源所在路径
    }).extend({
        index: 'lib/index' //主入口模块
    }).use(['index', 'user'], function () {
        var $ = layui.$
            , setter = layui.setter
            , admin = layui.admin
            , form = layui.form
            , router = layui.router();

        form.render();

        //找回密码下一步
        form.on('submit(LAY-user-forget-submit)', function (obj) {
            var button = $("#LAY-user-forget-submit");
            button.addClass('layui-btn-disabled').attr("disabled", true);
            //请求登入接口
            $.ajax({
                url: "",
                data: obj.field,
                type: "POST",
                success: function (data) {
                    if (data.msg === "success") {
                        layer.msg('成功', {
                            offset: '15px'
                            , icon: 1
                            , time: 1000
                        }, function () {
                            location.href = '{{url('manager_forget')}}' + '?token=' + data.token + '&phone=' + data.phone + '#/type=resetpass'
                        });
                    } else {
                        layer.msg("登录失败" + "<br/>" + data.data.error, {
                            offset: '100px'
                            , icon: 2
                            , time: 1500
                        }, function () {
                            $("#get-captcha").click();
                            button.removeClass('layui-btn-disabled').attr("disabled", false);
                        });
                    }
                },
                error: function (data) {
                    var errors = '';
                    $.each(data.responseJSON.data, function (index, value) {
                        errors += index + 1 + ": " + value + "<br />"
                    });

                    layer.msg('错误' + "<br/>" + errors, {
                        offset: '100px',
                        icon: 2
                        , time: 1500
                    }, function () {
                        $("#get-captcha").click();
                        button.removeClass('layui-btn-disabled').attr("disabled", false);
                    });
                }
            });
        });

        //重置密码
        form.on('submit(LAY-user-forget-resetpass)', function (obj) {
            var button = $("#LAY-user-forget-resetpass");
            button.addClass('layui-btn-disabled').attr("disabled", true);
            //请求登入接口
            $.ajax({
                url: "",
                data: obj.field,
                type: "POST",
                success: function (data) {
                    if (data.msg === "success") {
                        layer.msg('成功', {
                            offset: '15px'
                            , icon: 1
                            , time: 1000
                        }, function () {
                            location.href = 'login';
                           // console.log('重置成功')
                        });
                    } else {
                        layer.msg("登录失败" + "<br/>" + data.data.error, {
                            offset: '100px'
                            , icon: 2
                            , time: 1500
                        }, function () {
                            button.removeClass('layui-btn-disabled').attr("disabled", false);
                        });
                    }
                },
                error: function (data) {
                    var errors = '';
                    $.each(data.responseJSON.data, function (index, value) {
                        errors += index + 1 + ": " + value + "<br />"
                    });
                    layer.msg('错误' + "<br/>" + errors, {
                        offset: '100px',
                        icon: 2
                        , time: 1500
                    }, function () {
                        button.removeClass('layui-btn-disabled').attr("disabled", false);
                    });
                }
            });
        });
        //发送验证码
        $('#get-sms-code').click(function () {
            //短信请求
            var button = $('#get-sms-code');
            var phone = $('#phone').val();
            var captcha = $('#captcha').val();
            if (captcha == '' || phone == '') {
                layer.msg('错误 <br/> 请输入 手机号和验证码', {
                    offset: '15px'
                    , icon: 2
                    , time: 1500
                });
                return false;
            }
            console.log('send-sms', phone, captcha);
            button.addClass('layui-btn-disabled').attr("disabled", true);
            $.ajax({
                url: "send_sms",
                data: {_token: '{{csrf_token()}}', phone: phone, captcha: captcha},
                type: "POST",
                success: function (data) {
                    if (data.msg === "success") {
                        layer.msg('发送成功', {
                            offset: '15px'
                            , icon: 1
                            , time: 1000
                        }, function () {
                            sms_code_timer(button);
                            // location.href = '{{url('forget')}}' + '#/type=resetpass'
                        });
                    } else {
                        layer.msg("发送失败" + "<br/>" + data.data.error, {
                            offset: '100px'
                            , icon: 2
                            , time: 1500
                        }, function () {
                            $('#get-captcha').attr('src', "{{captcha_src()}}" + "&t=" + (new Date()).valueOf())
                            button.removeClass('layui-btn-disabled').attr("disabled", false);
                        });
                    }
                },
                error: function (data) {
                    var errors = '';
                    $.each(data.responseJSON.data, function (index, value) {
                        errors += index + 1 + ": " + value + "<br />"
                    });
                    if (data.status === 429) {
                        errors += "操作过于频繁 <br />"
                    }
                    layer.msg('错误' + "<br/>" + errors, {
                        offset: '100px',
                        icon: 2
                        , time: 1500
                    }, function () {
                        $('#get-captcha').attr('src', "{{captcha_src()}}" + "&t=" + (new Date()).valueOf())
                        button.removeClass('layui-btn-disabled').attr("disabled", false);
                    });
                }
            });

        })
        //回车提交
        $('#LAY-user-login-vercode').on('keydown', function (event) {
            if (event.keyCode === 13) {
                $("#LAY-user-login-submit").click();
            }
        });
        //更新CAPTCHA
        $('#get-captcha').click(function () {
            this.src = "{{captcha_src()}}" + "&t=" + (new Date()).valueOf();
        })

    });
    //验证码倒计时
    var time = 5;

    function sms_code_timer(obj) {
        if (time === 1) {
            obj.text('获取验证码');
            obj.removeClass('layui-btn-disabled').attr("disabled", false);
            time = 5;
            return
        }
        if (time > 0) {
            time--;
            obj.text('重新获取 ' + time);
            setTimeout(function () {
                sms_code_timer(obj)
            }, 1000)
        }
    }
</script>
